package com.damon.dm_mail.mail;

import com.damon.dm_mail.controller.ScheduleTask;
import com.damon.dm_mail.pojo.Email;
import com.damon.dm_mail.pojo.Sender;
import com.damon.dm_mail.pojo.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

@Configuration
@PropertySource(value = "classpath:config/mail.properties", encoding = "utf-8")
public class MailConfig {
    @Value("${mail.auth}")
    private String auth;
    @Value("${mail.timeout}")
    private String time0ut;
    @Value("${mail.starttls.enable}")
    private String starttlsEnable;
    @Value("${mail.socketFactory.fallback}")
    private String socketFactoryFallback;
    @Value("${mail.socketFactory.class}")
    private String socketFactoryClass;

    @Value("${mail.host.163}")
    private String host163;

    @Value("${mail.port.163}")
    private String port163;

    @Value("${mail.host.qq}")
    private String hostqq;
    @Value("${mail.port.qq}")
    private String portqq;


    @Bean
    public JavaMailSenderImpl mailSender(Environment env) {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setDefaultEncoding("utf-8");
        Properties jpro = new Properties();
        jpro.setProperty("mail.smtp.auth", auth);
        jpro.setProperty("mail.smtp.timeout", time0ut);
        jpro.setProperty("mail.smtp.starttls.enable", starttlsEnable);
//        jpro.setProperty("mail.smtp.socketFactory.port", String.valueOf(mailPort));
        jpro.setProperty("mail.smtp.socketFactory.fallback", socketFactoryFallback);
        jpro.setProperty("mail.smtp.socketFactory.class", socketFactoryClass);
        mailSender.setJavaMailProperties(jpro);
        return mailSender;
    }

    /**
     * 获取完整的mailsender
     *
     * @param mailSender
     * @param prefix
     * @param from
     * @param to
     * @return
     * @throws MessagingException
     */
    public JavaMailSenderImpl sendEmail(JavaMailSenderImpl mailSender, String prefix, Sender from, UserInfo to, Email email) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
        helper.setFrom(from.getEmail());
        helper.setTo(to.getEmail());
//            sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
//            message.setSubject("=?GB2312?B?" + enc.encode(subject.getBytes()) + "?=");
        message.setSubject(email.getTitle());
        helper.setText(email.getContent(), true);      //true可以显示html格式的文本
        String host = "";
        String port = "";
        switch (prefix) {
            case "163.com":
                port = port163;
                host = host163;

                break;
            case "qq.com":
                port = portqq;
                host = hostqq;
                break;
            default:
                port = portqq;
                host = hostqq;
        }
        mailSender.setHost(host);
        mailSender.setPort(Integer.valueOf(port));

        mailSender.setUsername(from.getEmail());
        mailSender.setPassword(from.getPass());
        mailSender.getJavaMailProperties().setProperty("mail.smtp.socketFactory.port", port);
        mailSender.send(message);
        return mailSender;
    }
}
